//
//  DDConsumptionListVC.m
//  dingdang
//
//  Created by 阮 沧晖 on 15/7/18.
//  Copyright (c) 2015年 阮 沧晖. All rights reserved.
//

#import "DDConsumptionListVC.h"
#import "DDNetWorkAPIClient.h"
#import "DDGiftLog.h"

#import "ZCHeaderView.h"
#import "UIView+Extension.h"
#import "ZCListCell.h"
#import "StringUtil.h"

@interface DDConsumptionListVC ()<UITableViewDataSource,UITableViewDelegate>

{
    DDNetWorkAPIClient *network;
    UILabel *labCountIntegral;
}
@property (nonatomic,strong) ZCHeaderView *headView;
@property(nonatomic,weak)UITableView* tableview;
@property(nonatomic,strong)NSArray* detailArray;
//@property (weak, nonatomic) IBOutlet UILabel *labCountIntegral;
@end

@implementation DDConsumptionListVC

- (void)viewDidLoad {
    NSLog(@"进入到---------->DDConsumptionListVC");
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self createUI];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self createData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)createUI
{
    NSArray* array = @[@"时间",@"行为",@"积分数"];
    self.detailArray = @[
                         
                         ];
    ZCHeaderView *tableViewHeadView=[[ZCHeaderView alloc]initWithFrame:CGRectMake(0, 0,[UIScreen mainScreen].bounds.size.width, 40)];
    tableViewHeadView.labelTitleA = array;
    [tableViewHeadView bottomLineforViewWithColor:[UIColor lightGrayColor]];
    self.headView=tableViewHeadView;
    
//    UIScrollView *myScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,[UIScreen mainScreen].bounds.size.height-200)];
    
    UITableView *tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.headView.frame.size.width, [UIScreen mainScreen].bounds.size.height-245) style:UITableViewStyleGrouped];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.bounces=YES;
    tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    self.tableview=tableView;
    tableView.backgroundColor=[UIColor whiteColor];
    
//    myScrollView.bounces=NO;
//    myScrollView.contentSize=CGSizeMake(self.headView.frame.size.width,0);
//    myScrollView.scrollEnabled = YES;
    //[self.view addSubview:myScrollView];
    
    self.tableview.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0.01)];
    
    [self.view addSubview:tableView];
    
    labCountIntegral = [[UILabel alloc] initWithFrame:CGRectMake(0, tableView.frame.size.height, [UIScreen mainScreen].bounds.size.width, 40)];
    labCountIntegral.backgroundColor = [StringUtil colorWithHexString:@"DCDCDC"];
    labCountIntegral.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:labCountIntegral];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.detailArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZCListCell* cell = [ZCListCell cellWithTableView:tableView];
    
    cell.labelTitleA = self.detailArray[indexPath.row];
    
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    return self.headView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    
    return self.headView.frame.size.height;
}
- (NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}
- (BOOL)shouldAutorotate
{
    return YES;
}

-(void)createData
{
    network = [[DDNetWorkAPIClient alloc] init];
    [network giftQuery:@{@"logType":_logType} completionBlock:^(BOOL isSuccess, NSDictionary *dict, NSString *message, NSString *status) {
        NSLog(@"%@",dict);
        NSError *error ;
        DDGiftLog *log = [[DDGiftLog alloc] initWithDictionary:dict error:&error];
        
        if (error) {
            NSLog(@"%@",error.userInfo);
        }
        
        _rowsArray = [[NSMutableArray alloc] init];
        int integralCount = 0;
        for (int i=0; i<log.list.count; i++) {
            DDGiftLogCell *cell = log.list[i];
            NSString *fenzhi =  cell.integral==nil?@"":cell.integral;;
            NSMutableArray *array = [NSMutableArray array];
            [array addObject:cell.logTime==nil?@"":[self convertTime:cell.logTime]];
            [array addObject:cell.desc==nil?@"":cell.desc];
            [array addObject:fenzhi];
            [_rowsArray addObject:array];
            integralCount = [cell.integral intValue] + integralCount;
        }
        
        NSString *countIntegralStr;
        if ([_logType isEqualToString:@"1"]) {
            countIntegralStr = [NSString stringWithFormat:@"累计获取积分数:%d", integralCount];
        }else if ([_logType isEqualToString:@"2"]) {
            countIntegralStr = [NSString stringWithFormat:@"累计消费积分数:%.0f", fabs(integralCount)];
        }
        labCountIntegral.text = countIntegralStr;
        
        _detailArray = [NSArray arrayWithArray:_rowsArray];
        [self.tableview reloadData];
        
    }];
}


- (NSString *)convertTime:(NSString *)time
{
    if (!time || [@"" isEqualToString:time]) {
        return @"";
    }
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
        //实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];
        //用[NSDate date]可以获取系统当前时间
        NSDate *d = [NSDate dateWithTimeIntervalSince1970:time.longLongValue/1000];
        NSString *dateStr = [dateFormatter stringFromDate:d];
        return dateStr;
    
    return @"";
}
//
//
//#pragma mark - ASFTableViewDelegate
//- (void)ASFTableView:(ASFTableView *)tableView DidSelectRow:(NSDictionary *)rowDict WithRowIndex:(NSUInteger)rowIndex {
//    NSLog(@"%d", rowIndex);
//}

@end
